package org.lakesidecdc.outreach.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class LakesideOutreachManager implements EntryPoint, ResizeHandler {
	private static LakesideOutreachManager singleton;
	
	//private static final Images images = GWT.create(Images.class);
	
	public static LakesideOutreachManager get() {
		return singleton;
	}
	
	private ProjectList projectList;
	private ProjectDetail projectDetail = new ProjectDetail();
	
	private TopPanel topPanel = new TopPanel();
	private VerticalPanel rightPanel = new VerticalPanel();
		
	// Add projects panel and form
	private PopupPanel addProjectPanel = new PopupPanel();
	private VerticalPanel addProjectForm = new VerticalPanel();
	private FlexTable addProjectFormTable = new FlexTable();
	private TextBox projectNameTextBox = new TextBox();
	private RichTextArea projectDescriptionTextArea = new RichTextArea();
	private Button projectSaveButton = new Button("Save");
	private Label addProjectHeading = new Label();
	private Button addProjectButton = new Button("+");
	
	// Login details
	private LoginInfo loginInfo = null;
	private VerticalPanel loginPanel = new VerticalPanel();
	private Label loginLabel = new Label("Please sign in to your google account to access the Lakeside CDC Outreach Manager.");
	private Anchor signInLink = new Anchor("Sign In");
	
	/**
	 * Entry point method
	 */
	public void onModuleLoad() {
		// Check the login status using the login service
		LoginServiceAsync loginService = GWT.create(LoginService.class);
		loginService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {
			public void onFailure(Throwable error) {
				
			}
			
			public void onSuccess(LoginInfo result) {
				loginInfo = result;
				if(loginInfo.isLoggedIn()) {
					loadLakesideOutreachManager();
				} else {
					loadLogin();
				}
			}
		});
	}
	
	private void loadLogin() {
		// Assemble login panel
		signInLink.setHref(loginInfo.getLoginUrl());
		loginPanel.add(loginLabel);
		loginPanel.add(signInLink);
		RootPanel.get("lom").add(loginPanel);
	}
	
	private void loadLakesideOutreachManager() {
		// set sign out link
		topPanel.setSignOutLink(loginInfo.getLogoutUrl());
		
		singleton = this;
		
		topPanel.setWidth("100%"); // NOT DONE
		
		projectList = new ProjectList();
		projectList.setWidth("100%");
		
		rightPanel.add(projectList);
		rightPanel.add(projectDetail);
		projectList.setWidth("100%");
		projectDetail.setWidth("100%");
		
		DockPanel outer = new DockPanel();
		outer.add(topPanel, DockPanel.NORTH);
		outer.add(rightPanel, DockPanel.CENTER);
		outer.setWidth("100%");
		
		outer.setSpacing(4);
		outer.setCellWidth(rightPanel, "100%");
		
		// Hook the window resize event
		Window.addResizeHandler(this);
		
		Window.enableScrolling(false);
		Window.setMargin("0px");
		
		RootPanel.get().add(outer);
		
		DeferredCommand.addCommand(new Command() {
			public void execute() {
				onWindowResized(Window.getClientWidth(), Window.getClientHeight());
			}
		});
		
		onWindowResized(Window.getClientWidth(), Window.getClientHeight());
		
		// Assemble add project panel
		addProjectFormTable.setText(0, 0, "Project Name: ");
		addProjectFormTable.setWidget(0, 1, projectNameTextBox);
		addProjectFormTable.setText(1, 0, "Description: ");
		addProjectFormTable.setWidget(1, 1, projectDescriptionTextArea);
		addProjectFormTable.setWidget(2, 1, projectSaveButton);
		addProjectHeading.setText("Add Project");
		addProjectForm.add(addProjectHeading);
		addProjectForm.add(addProjectFormTable);
		addProjectPanel.add(addProjectForm);

		// Listen for mouse events on the Add button
		addProjectButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addProjectPanel.show();
			}
		});
		
		// Listen for mouse events on the Save Project button.
		projectSaveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addProject();
			}
		});
		
	}
	
	private void addProject() {
		final String name = projectNameTextBox.getText().trim();
		final String description = projectDescriptionTextArea.getText().trim();
		
		if(name.equals("") || name.length() < 1) {
			Window.alert("Please enter a name for your project.");
			projectNameTextBox.setFocus(true);
			return;
		}
		
		final Project project = new Project(name, description);
		
		if(!projectList.addProject(project)) {
			// Don't add to the table if it already exists
			Window.alert("Project name '" + name + "' already exists in the system.");
			projectNameTextBox.selectAll();
			return;
		}
		
		projectNameTextBox.setText("");
		projectDescriptionTextArea.setText("");
		
		// hide the add project panel
		addProjectPanel.hide();
		
		// update the project list
		projectList.update();
	}
	
	public void displayProject(Project project) {
		projectDetail.setProject(project);
	}
	
	public void onResize(ResizeEvent event) {
		onWindowResized(event.getWidth(), event.getHeight());
	}
	
	public void onWindowResized(int width, int height) {
		// Adjust detail panel size
		projectDetail.adjustSize(width, height);
	}
	
	public PopupPanel getAddProjectPanel() {
		return addProjectPanel;
	}
	
//	public EventList getCurrentEventList() {
//		return projectDetail.getEventList();
//	}
	
}
